<?php
/**
 * Created by PhpStorm.
 * User: 丶晓
 * Date: 2017/8/24
 * Time: 16:59
 */

namespace app\home\common;


class Tools
{
    public function success($msg, $data = null) // 正确返回格式
    {
        if (empty($data)) {
            return ['state' => true, 'msg' => $msg];
        }
        return ['state' => true, 'data' => $data];
    }

    public function error($msg, $data = null) // 错误返回格式
    {
        if (empty($data)) {
            return ['state' => false, 'msg' => $msg];
        }
        return ['state' => false, 'data' => $data];
    }

    function objarray_to_array($obj)
    {
        $ret = array();
        foreach ($obj as $key => $value) {
            if (gettype($value) == "array" || gettype($value) == "object") {
                $ret[$key] = $this->objarray_to_array($value);
            } else {
                $ret[$key] = $value;
            }
        }
        return $ret;
    }

    public function get_Logisticsinfos($typeCom,$typeNu){
        $post_data = array();
        $typeCom = input('get.com'); //快递公司编号
        $typeNu = input('get.num'); //快递单号
        $post_data["customer"] = 'AAC2911070F2569E44E295C5478FCAE5';
        $key= 'XUpySPHX873' ;
        $post_data["param"] = "{'com':'$typeCom','num':'$typeNu'}";
        $url='http://poll.kuaidi100.com/poll/query.do?';
        $post_data["sign"] = md5($post_data["param"].$key.$post_data["customer"]);
        $post_data["sign"] = strtoupper($post_data["sign"]);
        $o="";
        foreach ($post_data as $k=>$v)
        {
            $o.= "$k=".urlencode($v)."&";		//默认UTF-8编码格式
        }
        $post_data=substr($o,0,-1);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $result = curl_exec($ch);
        $data = json_decode($result,true);
        return $data;
    }


}